<template>
    <div>
        <myheader/>
        选择分类<br>
        <!-- 向子组件传参数 sondata里边把catalist放进去 -->
        <replay :data='catelist'/>

        <van-field
            v-model="form.name"
            name="name"
            label="名称"
            placeholder="名称"
            :rules="[{ required: true, message: '请填写名称' }]"
        />
         <van-field
            v-model="form.level"
            name="level"
            label="等级"
            placeholder="等级"
            :rules="[{ required: true, message: '请填写等级' }]"
        />
        <van-field
            v-model="form.params"
            name="params"
            label="参数"
            placeholder="参数"
        />
        <van-button color="linear-gradient(to right, #7232dd, #ffffff)" @click="add" style="width:200px">
                提交
        </van-button>
        {{catelist}}
        <br>
        {{list}}
    </div>
</template>

<script>
// 导入头部
import myheader from '@/components/myheader'
// 导入子组件
import replay from '@/components/replay'
    export default {
        data(){
            return{
                form:{parent:0},
                catelist:{},
                list:[]
            }
        },
        methods:{
            add(){
                // 获取父组件保存的父id,如果有就更新
                let pid = localStorage.getItem('pid')
                if(pid){
                    this.form.parent=pid
                }
                this.$axios.post('course/cateM',this.form)
                .then(res=>{
                    this.$toast.fail("添加成功")
                    this.form = {}
                    this.form.parent=0
                    this.getCatelist()
                    localStorage.removeItem('pid')
                })
            },
            getCatelist(){
                this.$axios.get('course/cateM')
                .then(res=>{
                    // 构建一个字典   数据结构字典套列表 一个顶级 son就是catelist
                    this.list = res.data.list
                    this.catelist={'id':0,'name':'','son':res.data.list}
                })
            },
        },
        mounted(){
            this.getCatelist()
        },
        //注册头部、子组件
        components:{
            'myheader':myheader,
            'replay':replay
        },
    }
</script>

<style scoped>

</style>